﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Channels;

namespace Client
{
    class Program
    {
        public class CallbackHandler : ICalculatorDuplexCallback
        {
            public void Equation(string equation)
            {
                Console.WriteLine("Equation({0})", equation);
            }

            public void Equals(double result)
            {
                Console.WriteLine("Result ({0})", result);
            }
        }


        static void Main(string[] args)
        {
            //MessageHeader myHeader = MessageHeader.CreateHeader(

            //svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service

            // Picks up configuration from the config file.

            CalculatorDuplexClient wcfClient
              = new CalculatorDuplexClient(new InstanceContext(new CallbackHandler()));
            try
            {
                // Call the AddTo service operation.
                double value = 100.00D;
                wcfClient.AddTo(value);

                // Call the SubtractFrom service operation.
                value = 50.00D;
                wcfClient.SubtractFrom(value);

                // Call the MultiplyBy service operation.
                value = 17.65D;
                wcfClient.MultiplyBy(value);

                // Call the DivideBy service operation.
                value = 2.00D;
                wcfClient.DivideBy(value);

                // Complete equation.
                wcfClient.Clear();

                // Wait for callback messages to complete before
                // closing.
                System.Threading.Thread.Sleep(5000);

                // Close the WCF client.
                wcfClient.Close();
                Console.WriteLine("Done!");
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. " + timeProblem.Message);
                wcfClient.Abort();
                Console.Read();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                wcfClient.Abort();
                Console.Read();
            }




            #region oth2

            //// Create a client
            //WSHttpBinding binding = new WSHttpBinding();
            //EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/servicemodelsamples/service");
            //OneWayCalculatorClient client = new OneWayCalculatorClient(binding, epAddress);

            //// Call the Add service operation.
            //double value1 = 100.00D;
            //double value2 = 15.99D;
            //client.Add(value1, value2);
            //Console.WriteLine("Add({0},{1})", value1, value2);

            //// Call the Subtract service operation.
            //value1 = 145.00D;
            //value2 = 76.54D;
            //client.Subtract(value1, value2);
            //Console.WriteLine("Subtract({0},{1})", value1, value2);

            //// Call the Multiply service operation.
            //value1 = 9.00D;
            //value2 = 81.25D;
            //client.Multiply(value1, value2);
            //Console.WriteLine("Multiply({0},{1})", value1, value2);

            //// Call the Divide service operation.
            //value1 = 22.00D;
            //value2 = 7.00D;
            //client.Divide(value1, value2);
            //Console.WriteLine("Divide({0},{1})", value1, value2);

            //// Call the SayHello service operation
            //string name = "World";
            //string response = client.SayHello(name);
            //Console.WriteLine("SayHello([0])", name);
            //Console.WriteLine("SayHello() returned: " + response);

            #endregion


            #region oth1

            ////Step 1: Create an endpoint address and an instance of the WCF Client.
            //CalculatorClient client = new CalculatorClient();


            //// Step 2: Call the service operations.
            //// Call the Add service operation.
            //double value1 = 100.00D;
            //double value2 = 15.99D;
            //double result = client.Add(value1, value2);
            //Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

            //// Call the Subtract service operation.
            //value1 = 145.00D;
            //value2 = 76.54D;
            //result = client.Subtract(value1, value2);
            //Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            //// Call the Multiply service operation.
            //value1 = 9.00D;
            //value2 = 81.25D;
            //result = client.Multiply(value1, value2);
            //Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            //// Call the Divide service operation.
            //value1 = 22.00D;
            //value2 = 7.00D;
            //result = client.Divide(value1, value2);
            //Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            ////Step 3: Closing the client gracefully closes the connection and cleans up resources.
            //client.Close();


            #endregion

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();

        }
    }
}
